log using "$rootpath/log/Fig_1.log", replace
//Figure 1 final run
clear

use "$rootpath/processed_data/Estimation_Data.dta", clear
//drop if tot_damage>1e+08
summarize tot_damage if max_frv_year>0&max_hpv_year==0&year<2015&year>2009, detail
gen frv_damage=r(mean)*(1/1000000)
summarize tot_damage if max_hpv_year>0&max_transfered_code==0&year<2015&year>2009, detail
gen hpv_excl_damage=r(mean)*(1/1000000)
summarize tot_damage if max_hpv_year>0&max_transfered_code==1&year<2015&year>2009, detail
gen hpv_kept_damage=r(mean)*(1/1000000)
summarize tot_damage if max_hpv_year>0&year<2015&year>2009&max_transfered_code!=., detail
gen hpv_damage=r(mean)*(1/1000000)
summarize tot_damage if max_hpv_year==0&max_frv_year==0&year<2015&year>2009, detail
gen other_damage=r(mean)*(1/1000000)


//p1bar
//graph bar other_damage frv_damage hpv_damage hpv_kept_damage hpv_excl_damage, title("Average Damage Per Plant, 2010-2014") ytitle("Damages per Plant (millions 2018 USD)") yscale(r(0 50)) outergap(2) bargap(0) intensity(100) bar(1, color(green)) bar(2, color(red)) bar(3, color(navy))  bar(4, color(orange)) bar(5, color(black)) graphregion(color(white)) legend(textwidth(95) size(*0.75) span rows(3) order(1 "Compliant plants" 2 "Noncompliant non-HPV plants" 3 "HPV plants" 4 "HPV plants with non-excluded priority violations" 5 "HPV plants with excluded priority violations"))
graph bar other_damage frv_damage hpv_damage hpv_kept_damage hpv_excl_damage, title("Average Damage Per Plant, 2010-2014") ytitle("Damages per Plant (millions 2018 USD)") yscale(r(0 50)) outergap(2) bargap(0)  bar(1, color(green) fintensity(20)) bar(2, color(red) fintensity(40)) bar(3, color(navy) fintensity(60))  bar(4, color(orange) fintensity(80)) bar(5, color(black)) graphregion(color(white)) legend(textwidth(95) size(*0.75) span rows(3) order(1 "Compliant plants" 2 "Noncompliant non-HPV plants" 3 "HPV plants" 4 "HPV plants with non-excluded priority violations" 5 "HPV plants with excluded priority violations"))

graph export "$rootpath/table_graph/Figure_One.pdf", replace

clear

use "$rootpath/processed_data/Estimation_Data.dta", clear

replace tot_damage=tot_damage*(1/1000000)
///This is the 80th percentile
//summarize tot_damage if max_frv_year>0&max_hpv_year==0&year<2015&year>2009, detail
gen frv_damage80temp=tot_damage if max_frv_year>0&max_hpv_year==0&year<2015&year>2009
egen frv_damage80temp2=pctile(frv_damage80temp), p(80)
summarize tot_damage if max_frv_year>0&max_hpv_year==0&year<2015&year>2009&tot_damage>frv_damage80temp2
gen frv_damage80X=r(mean)
//summarize tot_damage if max_hpv_year>0&max_transfered_code==0&year<2015&year>2009, detail

gen hpv_excl_damage80temp=tot_damage if max_hpv_year>0&max_transfered_code==0&year<2015&year>2009
egen hpv_excl_damage80temp2=pctile(hpv_excl_damage80temp), p(80)
summarize tot_damage if max_hpv_year>0&max_transfered_code==0&year<2015&year>2009&tot_damage>hpv_excl_damage80temp2
gen hpv_excl_damage80X=r(mean)
//summarize tot_damage if max_hpv_year>0&max_transfered_code==1&year<2015&year>2009, detail
 
gen hpv_kept_damage80temp=tot_damage if max_hpv_year>0&max_transfered_code==1&year<2015&year>2009
egen hpv_kept_damage80temp2=pctile(hpv_kept_damage80temp), p(80)
summarize tot_damage if max_hpv_year>0&max_transfered_code==1&year<2015&year>2009&tot_damage>hpv_kept_damage80temp2
gen hpv_kept_damage80X=r(mean)
 
//summarize tot_damage if max_hpv_year>0&year<2015&year>2009&max_transfered_code!=., detail

gen hpv_damage80temp=tot_damage if max_hpv_year>0&year<2015&year>2009&max_transfered_code!=.
egen hpv_damage80temp2=pctile(hpv_damage80temp), p(80)
summarize tot_damage if max_hpv_year>0&year<2015&year>2009&max_transfered_code!=.&tot_damage>hpv_damage80temp2
gen hpv_damage80X=r(mean)
//summarize tot_damage if max_hpv_year==0&max_frv_year==0&year<2015&year>2009, detail
 
gen other_damage80temp=tot_damage if max_hpv_year==0&max_frv_year==0&year<2015&year>2009
egen other_damage80temp2=pctile(other_damage80temp), p(80)
summarize tot_damage if tot_damage>other_damage80temp2&max_hpv_year==0&max_frv_year==0&year<2015&year>2009
gen other_damage80X=r(mean)

graph bar other_damage80X frv_damage80X hpv_damage80X hpv_kept_damage80X hpv_excl_damage80X, title("80th Percentile Plant Damages, 2010-2014") ytitle("Damages per Plant (millions 2018 USD)") yscale(r(0 20)) outergap(2) bargap(0) bar(1, color(green) fintensity(20)) bar(2, color(red) fintensity(40)) bar(3, color(navy) fintensity(60))  bar(4, color(orange) fintensity(80)) bar(5, color(black)) graphregion(color(white)) legend(textwidth(95) size(*0.75) span rows(3) order(1 "Compliant plants" 2 "Noncompliant non-HPV plants" 3 "HPV plants" 4 "HPV plants with non-excluded priority violations" 5 "HPV plants with excluded priority violations"))
graph export "$rootpath/table_graph/Figure_One_80th.pdf", replace

log close
